<?php

/**
 * Description of Rsa
 *
 */

namespace app\api\controller;
use think\Db;
class GaoDeTsapi extends Base
{

//put your code here
//创建服务
    static public $service_add = "https://tsapi.amap.com/v1/track/service/add";
//创建轨迹
    static public $trace_add = "https://tsapi.amap.com/v1/track/trace/add";
//里程和轨迹查询
    static public $terminal = "https://tsapi.amap.com/v1/track/terminal/trsearch";
//创建终端
    static public $terminal_add = "https://tsapi.amap.com/v1/track/terminal/add";
//上传点
    static public $point_upload = "https://tsapi.amap.com/v1/track/point/upload";

    /**
     * 构造函数
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

//创建服务

    /**
     * $data["key"]=用户在高德地图官网申请Web服务API类型KEY
     * * $data["name"]=Service 的名字，名字在同一个 Key 下不可重复，不可为空。
    命名规则：仅支持中文、英文大小字母、英文下划线"_"、英文横线"-"和数字,不能以"_"开头，最长不得超过128个字符。
     * * $data["desc"]=针对此 Service 的文字描述，方便用户对 Service 进行记忆。
    命名规则：仅支持中文、英文大小字母、英文下划线"_"、英文横线"-"和数字, 不能以"_"开头，最长不得超过128个字符。
     * return sid 所属服务的唯一ID,name服务名称
     */
    public function serviceAdd($data = array()) {
        if (empty($data["name"])) {
            $data["name"] = "weihom". rand(100000, 900000);
        }
        $res = httpRequest(self::$service_add, http_build_query($data));
        $res = json_decode($res, true);
        if ($res["errcode"] == "10000") {
            return array("success" => true, "data" => $res["data"]);
        } else {
            return array("success" => false, "message" => "serviceAdd".$res["errmsg"]);
        }
    }

    /**
     * 创建终端
     * $data["key"]=用户在高德地图官网申请Web服务API类型KEY
     * $data["name"]=Service 的名字，名字在同一个 Key 下不可重复，不可为空。
    命名规则：仅支持中文、英文大小字母、英文下划线"_"、英文横线"-"和数字,不能以"_"开头，最长不得超过128个字符。
     * $data["sid"]=针对此 Service 的文字描述，方便用户对 Service 进行记忆。
    命名规则：仅支持中文、英文大小字母、英文下划线"_"、英文横线"-"和数字, 不能以"_"开头，最长不得超过128个字符。
     * @param type $data
     * return sid 终端所属service的id,tid终端id,name终端的名称
     */
    public function terminalAdd($data = array()) {
        $res = httpRequest(self::$terminal_add, http_build_query($data));
        $res = json_decode($res, true);
        if ($res["errcode"] == "10000") {
            return array("success" => true, "data" => $res["data"]);
        } else {
            return array("success" => false, "message" => "terminalAdd".$res["errmsg"]);
        }
    }

    /**
     * 批量上传点
     * $data["key"]:用户在高德地图官网申请Web服务API类型KEY， $data["sid"]:服务的唯一编号，$data["tid"]:此ID终端唯一编号，$data["trid"]:此ID为轨迹的唯一编号
     * $data["points"]：array("location"=>array("x","y"),"locatetime"=>"")
     * @param type $data
     * @return type
     */
    public function uploadTraceAdd($data = array()) {
        $points = json_decode($data["points"], true);
        $newPoints = array();
        $i = 0;
        foreach ($points as $k => $v) {
            if ($k %90 == 0 && $k > 0) {
                $i++;
            }
            $newPoints[$i][] = $v;
        }
        foreach ($newPoints as $v) {
            $data["points"] = json_encode($v);
            $res = httpRequest(self::$point_upload, http_build_query($data));
            $res = json_decode($res, true);
// if ($res["errcode"] != "10000") {
// return array("success" => false, "message" => "uploadTraceAdd".$res["errmsg"]);
// }
        }
        return array("success" => true, "data" =>"");
    }

    /* * 创建
    * $data["key"]:用户在高德地图官网申请Web服务API类型KEY， $data["sid"]:服务的唯一编号，$data["tid"]:此ID终端唯一编号，$data["trid"]:此ID为轨迹的唯一编号
    * $data["points"]：array("location"=>array("x","y"),"locatetime"=>"")
    * @param type $data
    * @return type
    */

    public function traceAdd($data = array()) {
        $res = httpRequest(self::$trace_add, http_build_query($data));
        $res = json_decode($res, true);

        if ($res["errcode"] == "10000") {
            return array("success" => true, "data" => $res["data"]);
        } else {
            return array("success" => false, "message" => "traceAdd" . $res["errmsg"]);
        }
    }

    /**
     * 查询里程
     * $data["key"]:用户在高德地图官网申请Web服务API类型KEY， $data["sid"]:服务的唯一编号，$data["tid"]:此ID终端唯一编号，$data["trid"]:此ID为轨迹的唯一编号
     * $data["points"]：array("location"=>array("x","y"),"locatetime"=>"")
     * @param type $data
     * @return distance:轨迹长度，单位：米,time:轨迹持续时间，单位：毫秒,counts:返回的轨迹点数量
     */
    public function queryTrsearch($data = array()) {
        $string = "";
        $n = 0;
        $data["recoup"]=1;
        $data["gap"]=100;
        foreach ($data as $k => $v) {
            if ($n == 0) {
                $string .= "?" . $k . "=" . $v;
            } else {
                $string .= "&" . $k . "=" . $v;
            }
            $n++;
        }
        $res = httpRequest(self::$terminal . $string);
        $res = json_decode($res, true);
        if ($res["errcode"] == "10000") {
            return array("success" => true, "data" => $res["data"]["tracks"][0]);
        } else {
            return array("success" => false, "message" => "queryTrsearch" . $res["errmsg"]);
        }
    }
    public function oneQueryTrsearch($data = array()) {
        $string = "";
        $n = 0;
        $data["recoup"]=1;
        $data["gap"]=50;
        $data["ispoints"]=1;
        $data["pagesize"]=980;
        $data["mapmatch"]=1;
        $data["attribute"]=1;
        $data["rthreshold"]=20;
        foreach ($data as $k => $v) {
            if ($n == 0) {
                $string .= "?" . $k . "=" . $v;
            } else {
                $string .= "&" . $k . "=" . $v;
            }
            $n++;
        }
        $res = httpRequest(self::$terminal . $string);
        $res = json_decode($res, true);
        if ($res["errcode"] == "10000") {
            return array("success" => true, "data" => $res["data"]);
        } else {
            return array("success" => false, "message" => "queryTrsearch" . $res["errmsg"]);
        }
    }

}
